package one.stand.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.aop.ApiController;
import one.stand.model.ResultModel;
import one.stand.request.*;
import one.stand.response.CompanyResponse;
import one.stand.response.DynamicResponse;
import one.stand.response.IndustryResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.util.List;
import java.util.Map;

/**
 * @author: ZRH
 * @CreateDate: 2019/4/17 9:45
 */
@RestController
@RequestMapping("crmPc/companyGoods")
@Api(description = "crm-PC企业管理(产品管理)",tags="crm-PC企业管理(产品管理)")
@Slf4j
public class CompanyController extends BaseController{

    @PostMapping("/selectGoodsTypeInfoPC")
    @ApiOperation(value = "根据公司ID查询产品分类（包含商品信息）")
    @ApiImplicitParams({})
    @ApiController(isLogin = true,company = true,card = true,paramClass = GoodstypeRequest.class)
    public ResultModel<Map<String, Object>> selectGoodsTypeInfoPC(@RequestAttribute GoodstypeRequest request){

        return crmFegin.selectGoodsTypeInfoPC(request);
    }

    @PostMapping("/getGoodsTypeInfo")
    @ApiOperation(value = "查询简单商品分类（用于商品分类下拉列表）")
    @ApiImplicitParams({})
    @ApiController(isLogin = true,company = true,card = true,paramClass = GoodstypeRequest.class)
    public ResultModel getGoodsTypeInfo(@RequestAttribute GoodstypeRequest request){

        return crmFegin.getGoodsTypeInfo(request);
    }

    @PostMapping("/addGoodsType")
    @ApiOperation(value = "添加/修改商品分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodstypeId", value = "分类id", paramType = "Integer"),
            @ApiImplicitParam(name = "name", value = "分类名称", required = true, paramType = "String"),
    })
    @ApiController(isLogin = true,company = true,isBoss = true,card = true,paramClass = GoodstypeRequest.class)
    public ResultModel addGoodsType(@RequestAttribute GoodstypeRequest request){

        return crmFegin.addGoodsType(request);
    }

    @PostMapping("/delGoodsType")
    @ApiOperation(value = "删除商品分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodstypeId", value = "分类id", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,company = true,isBoss = true,card = true,paramClass = GoodstypeRequest.class)
    public ResultModel delGoodsType(@RequestAttribute GoodstypeRequest request){

        return crmFegin.delGoodsType(request);
    }

    @PostMapping("/moveGoodsType")
    @ApiOperation(value = "上移/下移商品分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodstypeId", value = "分类id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "type", value = "1上移，2下移", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,company = true,isBoss = true,card = true,paramClass = GoodstypeRequest.class)
    public ResultModel moveGoodsType(@RequestAttribute GoodstypeRequest request){
        return crmFegin.moveGoodsType(request);
    }

    @PostMapping("/getGoodsInfoPC")
    @ApiOperation(value = "查询商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品id", paramType = "Integer"),
    })
    @ApiController(isLogin = true,company = true,card = true,paramClass = GoodsRequest.class)
    public ResultModel<Map<String, Object>> getGoodsInfoPC(@RequestAttribute GoodsRequest request){
        return crmFegin.getGoodsInfoPC(request);
    }

    @PostMapping("/moveGoodsInfo")
    @ApiOperation(value = "上移/下移商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "type", value = "1上移，2下移", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,company = true,isBoss = true,card = true,paramClass = GoodsRequest.class)
    public ResultModel<Boolean> moveGoodsInfo(@RequestAttribute GoodsRequest request){
        return crmFegin.moveGoodsInfo(request);
    }

    @PostMapping("/addGoodsInfo")
    @ApiOperation(value = "添加/修改商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品id", paramType = "String"),
            @ApiImplicitParam(name = "goodsName", value = "商品名称", required = true, paramType = "String"),
            @ApiImplicitParam(name = "goodstypeId", value = "分类id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "goodsDetails", value = "商品详情", required = true, paramType = "String"),
            @ApiImplicitParam(name = "ispayment", value = "1线上支付，2其他", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "remark", value = "备注", paramType = "String"),
            @ApiImplicitParam(name = "specIds", value = "商品规格id（逗号隔开）", required = true, paramType = "String"),
            @ApiImplicitParam(name = "goodPhoto", value = "商品图片（逗号隔开）", required = true, paramType = "String"),
            @ApiImplicitParam(name = "coverImg", value = "产品封面图", required = true, paramType = "String"),
    })
    @ApiController(isLogin = true,company = true,isBoss = true,card = true,paramClass = GoodsRequest.class)
    public ResultModel addGoodsInfo(@RequestAttribute GoodsRequest request){

        return crmFegin.addGoodsInfo(request);
    }

    @PostMapping("/addGoodsInfoList")
    @ApiOperation(value = "修改商品信息（3.2版本迭代）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品id", paramType = "String"),
            @ApiImplicitParam(name = "goodsName", value = "商品名称", required = true, paramType = "String"),
            @ApiImplicitParam(name = "goodstypeId", value = "分类id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "goodsDetails", value = "商品详情", required = true, paramType = "String"),
            @ApiImplicitParam(name = "ispayment", value = "1线上支付，2其他", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "isDistribution", value = "0不是分销商品，1是", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "remark", value = "备注", paramType = "String"),
            @ApiImplicitParam(name = "goodPhoto", value = "商品图片（逗号隔开）", required = true, paramType = "String"),
            @ApiImplicitParam(name = "coverImg", value = "产品封面图", required = true, paramType = "String"),
            @ApiImplicitParam(name = "goodSpecRequestList", value = "商品规格list", required = true, paramType = "List"),
    })
    @ApiController(isLogin = true,company = true,card = true,isBoss = true,paramClass =GoodsRequest.class )
    public ResultModel addGoodsInfoList(@RequestAttribute GoodsRequest request){
        return crmFegin.addGoodsInfoList(request);
    }

    @PostMapping("/addGoodSpecInfo")
    @ApiOperation(value = "添加商品规格信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "specName", value = "类型名称", required = true, paramType = "String"),
            @ApiImplicitParam(name = "specAttribute", value = "属性名", required = true, paramType = "String"),
            @ApiImplicitParam(name = "specNum", value = "库存", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "price", value = "价格(分)", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "parentId", value = "父级id（如果是类型的第一个属性就给0，如果不是就给第一个属性的specId）", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,company = true,isBoss = true,card = true,paramClass = GoodSpecRequest.class)
    public ResultModel addGoodSpecInfo(@RequestAttribute GoodSpecRequest request){

        return crmFegin.addGoodSpecInfo(request);
    }

    @PostMapping("/transferGoods")
    @ApiOperation(value = "转移产品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "goodstypeId", value = "商品类型ID", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,company = true,isBoss = true,card = true,paramClass = GoodsRequest.class)
    public ResultModel transferGoods(@RequestAttribute GoodsRequest request){

        return crmFegin.transferGoods(request);
    }

    @PostMapping("/delGoodsInfo")
    @ApiOperation(value = "删除商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "isDel", value = "0上架 1下架", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,company = true,isBoss = true,card = true,paramClass = GoodsRequest.class)
    public ResultModel delGoodsInfo(@RequestAttribute GoodsRequest request){

        return crmFegin.delGoodsInfo(request);
    }

    @PostMapping("/delGoodSpecInfo")
    @ApiOperation(value = "删除商品规格信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "specId", value = "商品分类id", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,company = true,isBoss = true,card = true,paramClass = GoodSpecRequest.class)
    public ResultModel delGoodSpecInfo(@RequestAttribute GoodSpecRequest request){

        return crmFegin.delGoodSpecInfo(request);
    }

    @ApiOperation(value = "个人企业信息")
    @ApiController(isLogin = true,company = true,isParam = false)
    @PostMapping("getCompany")
    public ResultModel<CompanyResponse> getCompany(CompanyRequest request, @RequestAttribute TokenRequest tokenRequest) {
        BeanUtils.copyProperties(tokenRequest,request);
        return crmFegin.getCompany(request);
    }

    @ApiOperation("行业列表")
    @GetMapping("getList")
    @ApiController(isParam = false)
    public ResultModel<List<IndustryResponse>> getList() {
        return crmFegin.getList();
    }

    @ApiOperation(value = "修改企业")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyLogo", value = "企业logo", required = true, paramType = "String"),
            @ApiImplicitParam(name = "companyId", value = "企业id", required = true, paramType = "String"),
            @ApiImplicitParam(name = "companyName", value = "企业名", required = true, paramType = "String"),
            @ApiImplicitParam(name = "industryId", value = "行业id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "companyNotice", value = "企业公告", paramType = "String"),
            @ApiImplicitParam(name = "password", value = "企业口令", paramType = "String"),
    })
    @ApiController(isLogin = true, company = true,isBoss = true,paramClass = CompanyRequest.class)
    @PostMapping("updCompany")
    public ResultModel<Boolean> updCompany(@RequestAttribute CompanyRequest request) {
        return crmFegin.updCompany(request);
    }

    @ApiOperation(value = "crmPC-支付")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "merchantId", value = "商户id", required = true, paramType = "String"),
            @ApiImplicitParam(name = "key", value = "秘钥", required = true, paramType = "String"),
            @ApiImplicitParam(name = "certData", value = "文件", required = true, paramType = "String"),
    })
    @ApiController(isLogin = true, company = true,isBoss = true,paramClass = CompanyAccountRequest.class)
    @PostMapping("payment")
    public ResultModel<String> payment(@RequestAttribute CompanyAccountRequest request) {
        return crmFegin.payment(request);
    }

    @RequestMapping(value = "/backPayMent")
    @ApiController(isParam = false)
    public String backPayMent(HttpServletRequest request) {
        try {
            BufferedReader reader = request.getReader();
            String line = "";
            StringBuffer inputString = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                inputString.append(line);
            }
            request.getReader().close();
            return crmFegin.backPayMent(inputString.toString());
        }catch (Exception e){
            log.error("backPayMent[异常]", e);
        }
        return fail();
    }


    /**
     * 企业移交
     * @param request
     * @return
     */
    @ApiOperation(value = "crmPC-企业转移")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "转移目标用户id", required = true, paramType = "String"),
    })
    @ApiController(isLogin = true, company = true,isBoss = true,paramClass = CompanyRequest.class)
    @PostMapping("companyTransfer")
    public ResultModel<Boolean> companyTransfer(@RequestAttribute CompanyRequest request){
        return crmFegin.companyTransfer(request);
    }

    @ApiOperation(value = "查询代理商企业下公司的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyName", value = "公司名称", paramType = "String"),
})
    @PostMapping("selectAgentCompanyList")
    @ApiController(isLogin = true, company = true, paramClass = CompanyRequest.class)
    public ResultModel<Map<String, Object>> selectAgentCompanyList(@RequestAttribute CompanyRequest request) {
        return crmFegin.selectAgentCompanyList(request);
    }

    /**
     * 查询企业信息
     *
     * @param request
     * @return
     */
    @PostMapping("getAdminCompanyInfo")
    @ApiOperation(value = "查询代理商企业下公司的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "公司ID", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true, company = true, paramClass = CompanyRequest.class)
    public ResultModel<CompanyResponse> getAdminCompanyInfo(@RequestAttribute CompanyRequest request) {
        return crmFegin.getAdminCompanyInfo(request);
    }

    public String fail() {
        return "<xml> \n" +
                "  <return_code><![CDATA[fail]]></return_code>\n" +
                "  <return_msg><![CDATA[OK]]></return_msg>\n" +
                "</xml>";
    }


    @PostMapping("getDynamicInfo")
    @ApiOperation(value = "查询公司动态/文章详细信息")
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "dynamicId", value = "动态/文章id", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,company=true,paramClass = DynamicRequest.class)
    public ResultModel<Map<String, Object>> getDynamicInfo(@RequestAttribute DynamicRequest request) {
        return crmFegin.getDynamicInfoPC(request);
    }

    @PostMapping("addDynamicInfo")
    @ApiOperation(value = "添加/修改公司动态/文章")
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "companyId", value = "企业id", required = true, paramType = "Integer"),
                    @ApiImplicitParam(name = "title", value = "标题", required = true, paramType = "String"),
                    @ApiImplicitParam(name = "type", value = "类型 1:文章 2:动态 3:转载", required = true, paramType = "Integer"),
                    @ApiImplicitParam(name = "photos", value = "图片", paramType = "String"),
                    @ApiImplicitParam(name = "details", value = "详情", paramType = "String"),
                    @ApiImplicitParam(name = "author", value = "当前作者", paramType = "String"),
                    @ApiImplicitParam(name = "originAuthor", value = "原始作者，如转载文章的来源公众号", paramType = "String"),
            @ApiImplicitParam(name = "essayTypeId", value = "分类id", paramType = "Integer"),
            @ApiImplicitParam(name = "isDrafts", value = "是否发布", paramType = "Integer"),
    })
    @ApiController(isLogin = true,company=true,isBoss = true,paramClass = DynamicRequest.class)
    public ResultModel addDynamicInfo(@RequestAttribute DynamicRequest request) {
        return crmFegin.addDynamicInfo(request);
    }

    @PostMapping("updateDynamicState")
    @ApiOperation(value = "修改文章状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dynamicId", value = "动态id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "isDrafts", value = "是否发布", paramType = "Integer"),
    })
    @ApiController(isLogin = true,company=true,isBoss = true,paramClass = DynamicRequest.class)
    public ResultModel updateDynamicState(@RequestAttribute DynamicRequest request) {
        return crmFegin.updateDynamicState(request);
    }



    @PostMapping("getDynamicDetail")
    @ApiOperation(value = "动态/文章详情")
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "dynamicId", value = "文章、动态ID", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,company=true,isBoss = true,paramClass = DynamicRequest.class)
    public ResultModel getDynamicDetail(@RequestAttribute DynamicRequest request) {
        return crmFegin.getDynamicDetail(request);
    }


    @PostMapping("delDynamicInfo")
    @ApiOperation(value = "删除公司动态/文章")
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "dynamicId", value = "文章、动态ID", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,company=true,isBoss = true,paramClass = DynamicRequest.class)
    public ResultModel delDynamicInfo(@RequestAttribute DynamicRequest request) {
        return crmFegin.delDynamicInfo(request);
    }

}
